<?php
//action router
class BaseActionRouter
{
	public function execute($pageContext)
	{
		$actionName = 'IndexAction';
		$actionMethod = 'index';
		
		$c = $pageContext->get['c'];
		if($c) $actionData = explode("-", $c);
		
		$path = "";
		if($actionData)
		{
			$actionArr = explode("/", $actionData[0]);
			$actionName = array_pop($actionArr) . 'Action';
			$path = implode("/",$actionArr);
			if($actionData[1]) $actionMethod = $actionData[1]; //method
		}
		$actionPath = ACTION_BASE_PATH . '/' . ($path ? $path . '/' : '');
		$path = implode(".", explode("/", $actionPath)) . $actionName;
		
		define("ACTION_NAME", $actionName);
		define('ACTION_METHOD', $actionMethod);
		define('ACTION_URL', $c);
		define('ACTION_PATH', $path);
		
		$obj = ClassFactory::forName($path, $pageContext);
		if(!method_exists($obj, ACTION_METHOD)) throw new Exception('method ' . ACTION_NAME . '::' . ACTION_METHOD . '() not exist');
		
		pageLoad($pageContext);
		
		$obj->$actionMethod();
	}
	
	//组合url
	public function url($action, $params = array(), $file = '')
	{
		if($file)
		{
			$url = WEB_URL . "/$file?c=" . $action;
		}
		else
		{
			$url = WEB_URL . '/?c=' . $action;
		}
		
		if(!$params) return $url;
		
		$p = array();
		foreach($params as $k => $v)
		{
			$p[] = "$k=$v";
		}
		return $url . '&' . implode('&', $p);
	}
}
?>